package com.atguigu.springcloud.service.ServiceImpl;

import com.atguigu.springcloud.dao.OrderDao;
import com.atguigu.springcloud.domain.Order;
import com.atguigu.springcloud.service.AccountService;
import com.atguigu.springcloud.service.OrderService;
import com.atguigu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author :hucaiyuan
 * @date :2022/02/26   10:13
 * @description :
 */
@Component
@Service
@Slf4j
public class OrderServiceImpl implements OrderService
{
    @Resource
    private OrderDao orderDao;

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;


    @Override
    @GlobalTransactional(name = "fsp-order",rollbackFor = Exception.class)
    public void create(Order order) {
        //1.新建订单
        log.info("--------->创建订单!");
        orderDao.create(order);

        //2.扣减库存
        log.info("--->订单微服务调用库存做减扣!");
        storageService.decrease(order.getProductId(),order.getCount());

        //3.扣减账户余额
        log.info("--->订单微服务调用账户做减扣!");
        accountService.decrease(order.getUserId(),order.getMoney());

        //4.修改订单状态
        log.info("---->修改订单的状态!");
        orderDao.update(order.getUserId(),0);
    }
}

